package top.sdwu_top.kbase.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import top.sdwu_top.kbase.common.aspect.exception.MyException;
import top.sdwu_top.kbase.entity.KbaseAuthUser;
import top.sdwu_top.kbase.entity.KbaseFileComment;
import top.sdwu_top.kbase.mapper.KbaseFileCommentMapper;
import top.sdwu_top.kbase.service.IKbaseFileCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.sdwu_top.kbase.service.IKbaseFileVersionService;
import top.sdwu_top.kbase.vo.KbaseFileCommentVo;

import java.util.Objects;

/**
 * <p>
 * 文件评论 服务实现类
 * </p>
 *
 * @author zhipeng.duan@nvr-china.com
 * @since 2020-02-20
 */
@Service
public class KbaseFileCommentServiceImpl extends ServiceImpl<KbaseFileCommentMapper, KbaseFileComment> implements IKbaseFileCommentService {

    @Autowired
    IKbaseFileVersionService fileVersionService;

    @Override
    public boolean create(KbaseFileComment kbaseFileComment) {
        if (StringUtils.isBlank(kbaseFileComment.getFileId())) {
            throw new MyException(50003, "文件id不能为空");
        }
        if (Objects.isNull(kbaseFileComment.getVersion())) {
            kbaseFileComment.setVersion(fileVersionService.getLastVersion(kbaseFileComment.getFileId()));
        }
        return super.save(kbaseFileComment);
    }

    @Override
    public boolean update(KbaseFileComment kbaseFileComment) {
        if (StringUtils.isBlank(kbaseFileComment.getId())) {
            throw new MyException(50003, "更新id不能为空");
        }
        return super.updateById(kbaseFileComment);
    }

    @Override
    public boolean delete(String id) {
        if(!StringUtils.isNumeric(id)) {
            throw new MyException(50003, "id必须为纯数字");
        }
        return super.removeById(id);
    }

    @Override
    public Page<KbaseFileComment> page(KbaseAuthUser user, KbaseFileCommentVo commentVo) {
        if (StringUtils.isBlank(commentVo.getFileId())) {
            throw new MyException(50003, "文件id不能为空");
        }
        QueryWrapper<KbaseFileComment> qw = Wrappers.query();
        qw.eq(StringUtils.isNotBlank(commentVo.getCreateBy()), "CREATE_BY", commentVo.getCreateBy())
                .eq(StringUtils.isNotBlank(commentVo.getFileId()), "FILE_ID", commentVo.getFileId())
                .eq(StringUtils.isNotBlank(commentVo.getVersion()), "VERSION", commentVo.getVersion())
                .eq(Objects.nonNull(commentVo.getOnlyCreator()) && commentVo.getOnlyCreator(), "CREATE_BY", user.getId());
        return super.page(commentVo.getPage(), qw);
    }


}
